package zhangqi.com.guessmusic.myui;

/**
 * 自定义GridView类，继承GridView
 */
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import java.util.ArrayList;

import zhangqi.com.guessmusic.R;
import zhangqi.com.guessmusic.model.IWordButtonClickListener;
import zhangqi.com.guessmusic.model.WordButton;
import zhangqi.com.guessmusic.util.Util;
public class MyGridView extends GridView {

    public final static int COUNTS_WORDS =24;

    private ArrayList<WordButton> mArrayList = new ArrayList<WordButton>();//按钮容器

    private MyGridAdapter mAdapter ;//一定要有适配器

    private Context mContext;

    private Animation mSacleAnimation;//缩放动画---------次要

    //设置接口对象------------观察者模式
    private IWordButtonClickListener mWordButtonListener;

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mContext = context;

        mAdapter = new MyGridAdapter();
        this.setAdapter(mAdapter);//关联Adapter和GridView-------------重要
    }

    //  增加数据----更新数据
    public void updateData(ArrayList<WordButton> list){
        mArrayList =list;
        //重新设置数据源
        setAdapter(mAdapter);
    }

    //自定义Adapter
    class MyGridAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mArrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return mArrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int pos, View v, ViewGroup p) {
            final WordButton holder;//一般这里都默认起名是holder，从WordButton类中调用对象
            if(v==null){//默认写法，谷歌推荐
                v= Util.getView(mContext, R.layout.self_ui_gridview_item);//传入context
                holder = mArrayList.get(pos);
                //加载动画
                mSacleAnimation = AnimationUtils.loadAnimation(mContext,R.anim.scale);
                //设置动画的延迟时间
                mSacleAnimation.setStartOffset(pos*100);

                holder.mIndex = pos;

                holder.mViewButton = (Button) v.findViewById(R.id.item_btn);//得到button就是为了监听
                holder.mViewButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mWordButtonListener.onWordButtonClick(holder);
                    }
                });


                v.setTag(holder);//一定要“设置标签”setTag(),用于区分相似的View,将不同的holder传入
            }else {
                holder = (WordButton) v.getTag();//获取标签
            }

            holder.mViewButton.setText(holder.mWordString);//把文字放在按钮上

            //播放文字缩放动画
            v.startAnimation(mSacleAnimation);

            return v;
        }
    }

    /*
    * 注册监听接口
    * @param listener
    * */
    public void registOnWordButtonClick(IWordButtonClickListener listener){
        mWordButtonListener = listener;
    }
}
